/**
 * Created by Administrator on 2018/7/27/027.
 */

// layui方法
layui.use(['table', 'form', 'layer', 'vip_table'], function () {

    // 操作对象
    var form = layui.form
        , table = layui.table
        , layer = layui.layer
        , vipTable = layui.vip_table
        , $ = layui.jquery;

    var token = $('#_token').val();
    var tableIns;
    //初始化
    getReList();

    //工具条
    table.on('tool(re_data)', function(obj) {

        if(obj.event === 'edit') {
            var index = layer.load(2, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
                , time: 1000,
                end:function(e){
                    //自定页
                    layer.open({
                        type: 1,
                        title:['修改产品信息','font-size: 16px;color: #000;font-weight: bolder;'],
                        skin: 'layui-layer-rim', //加上边框
                        area: ['390px', '320px'], //宽高
                        content: $('.edit_re'),
                        success:function(){
                            $('#r_id').val(obj.data.id);
                            $('#money').val(obj.data.money);
                            $('#actual_account').val(obj.data.actual_account);
                            $('[name=status]').each(function(i,item){
                                if($(item).val()==obj.data.status){
                                    $(item).prop('checked',true);
                                }
                            });
                            form.render();
                        }
                    });
                }
            });
        }

        if(obj.event === 'del') {
            var index = layer.confirm('真的删除此账号,删除以后该账号信息将不存在,请谨慎操作', {
                btn: ['确定','取消'] //按钮
            }, function(){
                deleteRecharge(obj)
            })
        }
    });

    //添加提交
    form.on('submit(add_re)',function (data) {
        addRecharge(data);
    });

    //修改提交
    form.on('submit(edit_re)',function (data) {
        editRecharge(data);
    });



    // 刷新
    $('#btn-refresh').on('click', function () {
        tableIns.reload();
    });

    //添加套餐
    $('#btn-add').on('click',function(){
        //自定页
        layer.open({
            type: 1,
            title:['添加充值产品','font-size: 16px;color: #000;font-weight: bolder;'],
            skin: 'layui-layer-rim', //加上边框
            area: ['390px', '320px'], //宽高
            content: $('.add_re'),
            success:function(){

            }

        });
    });



    //列表
    function getReList(data) {
        // 表格渲染
        tableIns = table.render({
            elem: '#re_data',
            url: '/getRechargeList',
            page: true,
            count: 100,
            limit: 10,
            cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            , layout: ['count', 'prev', 'page', 'next', 'limit', 'skip']
            ,jump: function(obj){
                //console.log(obj)
            },
            cols: [
                [                  //标题栏
                    {field: 'money', title: '金额'}
                    , {field: 'actual_account', title: '实际到账'}
                    , {field: 'status_name', title: '状态'}
                    , {fixed: 'right', title: '操作', width: 150, align: 'center', toolbar: '#barOption'} //这里的toolbar值是模板元素的选择器
                ]
            ],
            where: {

            },
            method: 'get',
            done: function(res, curr, count) {
                //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
                console.log(res);

                //得到当前页码
                console.log(curr);

                //得到数据总量
                console.log(count);
            }

        });
    }

    //添加充值产品
    function addRecharge(data){
        var data = data.field;
        data._token = token;

        $.ajax({
            url: '/addRecharge',          //  添加套餐
            type: 'post',
            data: data,
            timeout: 1000,
            dataType: 'json',
            success: function (data, textStatus, jqXHR) {
                if (data.error_code == 0) {
                    layer.msg('添加成功',{
                        time: 2000, //2s后自动关闭
                        end:function(e){
                            location.href = "/recharge"
                        }
                    });
                } else {
                    layer.msg('添加失败，请重新添加',{
                        time: 2000, //2s后自动关闭
                        end:function(e){
                            location.href = "/recharge"
                        }
                    });
                }
            },
            error:function(){
                layer.msg('出错了',{
                    time: 2000,
                    end:function(e){
                        location.href = "/recharge"
                    }});
            }
        })

    }

    //删除充值产品
    function deleteRecharge(obj){

        $.ajax({
            url: '/deleteRecharge',          //  删除账号
            type: 'post',
            data: {
                recharge_id: obj.data.id,
                _token:token,
            },
            timeout: 5000,
            dataType: 'json',
            success: function (data, textStatus, jqXHR) {
                if (data.error_code == 0) {
                    layer.msg('删除成功',{
                        time: 2000, //2s后自动关闭
                        end:function(e){
                            location.href = "/recharge"
                        }
                    });
                } else {
                    layer.msg('删除失败，请重新操作',{
                        time: 2000, //2s后自动关闭
                        end:function(e){
                            location.href = "/recharge"
                        }
                    });
                }
            },
            error:function(){
                layer.msg('出错了',{
                    time: 2000,
                    end:function(e){
                        location.href = "/recharge"
                    }});
            }
        });


    }

    function editRecharge(data){
        var data = data.field;
        data._token = token;

        $.ajax({
            url: '/updateRecharge',          //  添加套餐
            type: 'post',
            data: data,
            timeout: 1000,
            dataType: 'json',
            success: function (data, textStatus, jqXHR) {
                if (data.error_code == 0) {
                    layer.msg('修改成功',{
                        time: 2000, //2s后自动关闭
                        end:function(e){
                            location.href = "/recharge"
                        }
                    });
                } else {
                    layer.msg('修改失败，请重新操作',{
                        time: 2000, //2s后自动关闭
                        end:function(e){
                            location.href = "/recharge"
                        }
                    });
                }
            },
            error:function(){
                layer.msg('出错了',{
                    time: 2000,
                    end:function(e){
                        location.href = "/recharge"
                    }});
            }
        })
    }

});